<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="投保人名称" prop="toubaoId">
        <el-select v-model="queryParams.toubaoId" placeholder="请选择投保人名称" clearable>
          <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="被保险人名称" prop="beibaoId">
        <el-select v-model="queryParams.beibaoId" placeholder="请选择被保险人名称" clearable>
          <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="保险人名称" prop="baoxianId">
        <el-select v-model="queryParams.baoxianId" placeholder="请选择保险人名称" clearable>
          <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="经纪/代理公司名称" prop="proxyId" label-width="130px">
        <el-select v-model="queryParams.proxyId" placeholder="请选择经纪/代理公司名称" clearable>
          <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="险种" prop="insureType">
        <el-input
            v-model="queryParams.insureType"
            placeholder="请输入险种"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['insurance:insureManage:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['insurance:insureManage:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['insurance:insureManage:remove']"
        >删除
        </el-button>
      </el-col>
      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--      type="warning"-->
      <!--      plain-->
      <!--      icon="el-icon-download"-->
      <!--      size="mini"-->
      <!--      @click="handleExport"-->
      <!--      v-hasPermi="['insurance:insureManage:export']"-->
      <!--  >导出</el-button>-->
      <!--</el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="insureManageList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="保险合同名称" align="center" prop="contractName" :min-width="100"/>
      <el-table-column label="保单号码" align="center" prop="insureNo" :min-width="100"/>
      <el-table-column label="投保人名称" align="center" prop="toubaoId" :min-width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.toubaoId"/>
        </template>
      </el-table-column>
      <el-table-column label="保险人名称" align="center" prop="baoxianId" :min-width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.baoxianId"/>
        </template>
      </el-table-column>
      <el-table-column label="被保险人名称" align="center" prop="beibaoId" :min-width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.beibaoId"/>
        </template>
      </el-table-column>
      <el-table-column label="经纪/代理公司名称" align="center" prop="proxyId" :min-width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.proxyId"/>
        </template>
      </el-table-column>
      <el-table-column label="险种" align="center" prop="insureType" :min-width="100"/>
      <el-table-column label="保单期限，X年X月" align="center" prop="insureLimit" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.insureLimit, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保险起期" align="center" prop="insureStartDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.insureStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保险止期" align="center" prop="insureEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.insureEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保险金额" align="center" prop="insureAmount" :min-width="100"/>
      <el-table-column label="保险费率" align="center" prop="insureRate" :min-width="100"/>
      <el-table-column label="保费，保险金额*保险费率" align="center" prop="premium" :min-width="100"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['insurance:insureManage:edit']"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['insurance:insureManage:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改保险主对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-divider content-position="center">合同主体信息</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="投保人名称" prop="toubaoId">
              <el-select v-model="form.toubaoId" placeholder="请选择投保人名称">
                <el-option
                    v-for="dict in dict.type.sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="被保险人名称" prop="beibaoId">
              <el-select v-model="form.beibaoId" placeholder="请选择被保险人名称">
                <el-option
                    v-for="dict in dict.type.sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="保险人名称" prop="baoxianId">
              <el-select v-model="form.baoxianId" placeholder="请选择保险人名称">
                <el-option
                    v-for="dict in dict.type.sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经纪/代理公司名称" prop="proxyId">
              <el-select v-model="form.proxyId" placeholder="请选择经纪/代理公司名称">
                <el-option
                    v-for="dict in dict.type.sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">保单信息</el-divider>
        <el-form-item label="保单号码" prop="insureNo">
          <el-input v-model="form.insureNo" placeholder="请输入保单号码"/>
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="保险合同名称" prop="contractName">
              <el-input v-model="form.contractName" placeholder="请输入保险合同名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="险种" prop="insureType">
              <el-input v-model="form.insureType" placeholder="请输入保险合同名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="保单期限，X年X月" prop="insureLimit">
          <el-date-picker clearable
                          v-model="form.insureLimit"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择保单期限，X年X月">
          </el-date-picker>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="保单起期" prop="insureStartDate">
              <el-date-picker clearable
                              v-model="form.insureStartDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择保单起期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保单止期" prop="insureEndDate">
              <el-date-picker clearable
                              v-model="form.insureEndDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择保单止期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="保险金额" prop="insureAmount">
              <el-input v-model="form.insureAmount" placeholder="请输入保险金额"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保险费率" prop="insureRate">
              <el-input v-model="form.insureRate" placeholder="请输入保险费率"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="保费" prop="premium">
          <el-input v-model="form.premium" readonly placeholder="请输入保费，保险金额*保险费率"/>
        </el-form-item>
        <el-divider content-position="center">保险合同回款计划信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTbInsuranceContractPremiumPlan">
              添加
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini"
                       @click="handleDeleteTbInsuranceContractPremiumPlan">删除
            </el-button>
          </el-col>
        </el-row>
        <el-table :data="tbInsuranceContractPremiumPlanList" :row-class-name="rowTbInsuranceContractPremiumPlanIndex"
                  @selection-change="handleTbInsuranceContractPremiumPlanSelectionChange"
                  ref="tbInsuranceContractPremiumPlan">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="所属保险id" prop="insureId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.insureId" placeholder="请输入所属保险id"/>
            </template>
          </el-table-column>
          <el-table-column label="批次名称" prop="planName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.planName" placeholder="请输入批次名称"/>
            </template>
          </el-table-column>
          <el-table-column label="计划回款金额" prop="planAmount" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.planAmount" placeholder="请输入计划回款金额"/>
            </template>
          </el-table-column>
          <el-table-column label="计划回款时间" prop="planDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.planDate" type="date" value-format="yyyy-MM-dd"
                              placeholder="请选择计划回款时间"/>
            </template>
          </el-table-column>
          <el-table-column label="回款比例" prop="rate" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.rate" placeholder="请输入回款比例"/>
            </template>
          </el-table-column>
          <el-table-column label="回款责任人" prop="responsiblePerson" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.responsiblePerson" placeholder="请输入回款责任人"/>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInsureManage,
  getInsureManage,
  delInsureManage,
  addInsureManage,
  updateInsureManage
} from "@/api/insurance/insureManage";

export default {
  name: "InsureManage",
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTbInsuranceContractPremiumPlan: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保险主表格数据
      insureManageList: [],
      // 保险合同回款计划表格数据
      tbInsuranceContractPremiumPlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        toubaoId: null,
        beibaoId: null,
        baoxianId: null,
        proxyId: null,
        insureNo: null,
        contractName: null,
        insureType: null,
        insureLimit: null,
        insureStartDate: null,
        insureEndDate: null,
        insureAmount: null,
        insureRate: null,
        premium: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "主键不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询保险主列表 */
    getList() {
      this.loading = false;
      // this.loading = true;
      // listInsureManage(this.queryParams).then(response => {
      //   this.insureManageList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        toubaoId: null,
        beibaoId: null,
        baoxianId: null,
        proxyId: null,
        insureNo: null,
        contractName: null,
        insureType: null,
        insureLimit: null,
        insureStartDate: null,
        insureEndDate: null,
        insureAmount: null,
        insureRate: null,
        premium: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.tbInsuranceContractPremiumPlanList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加保险主";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInsureManage(id).then(response => {
        this.form = response.data;
        this.tbInsuranceContractPremiumPlanList = response.data.tbInsuranceContractPremiumPlanList;
        this.open = true;
        this.title = "修改保险主";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tbInsuranceContractPremiumPlanList = this.tbInsuranceContractPremiumPlanList;
          if (this.form.id != null) {
            updateInsureManage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInsureManage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除保险主编号为"' + ids + '"的数据项？').then(function () {
        return delInsureManage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 保险合同回款计划序号 */
    rowTbInsuranceContractPremiumPlanIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 保险合同回款计划添加按钮操作 */
    handleAddTbInsuranceContractPremiumPlan() {
      let obj = {};
      obj.insureId = "";
      obj.planName = "";
      obj.planAmount = "";
      obj.planDate = "";
      obj.rate = "";
      obj.demand = "";
      obj.responsiblePerson = "";
      this.tbInsuranceContractPremiumPlanList.push(obj);
    },
    /** 保险合同回款计划删除按钮操作 */
    handleDeleteTbInsuranceContractPremiumPlan() {
      if (this.checkedTbInsuranceContractPremiumPlan.length == 0) {
        this.$modal.msgError("请先选择要删除的保险合同回款计划数据");
      } else {
        const tbInsuranceContractPremiumPlanList = this.tbInsuranceContractPremiumPlanList;
        const checkedTbInsuranceContractPremiumPlan = this.checkedTbInsuranceContractPremiumPlan;
        this.tbInsuranceContractPremiumPlanList = tbInsuranceContractPremiumPlanList.filter(function (item) {
          return checkedTbInsuranceContractPremiumPlan.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTbInsuranceContractPremiumPlanSelectionChange(selection) {
      this.checkedTbInsuranceContractPremiumPlan = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('insurance/insureManage/export', {
        ...this.queryParams
      }, `insureManage_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
